package com.jucai.pppeiyouclient.view.fragment;

import android.os.Handler;
import android.support.v7.widget.DefaultItemAnimator;
import android.support.v7.widget.LinearLayoutManager;
import android.text.TextUtils;
import android.view.View;

import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;
import com.jucai.pppeiyouclient.R;
import com.jucai.pppeiyouclient.adapter.RunningOrderAdapter;
import com.jucai.pppeiyouclient.base.BaseFragment;
import com.jucai.pppeiyouclient.base.CommonRecyclerViewAdapter;
import com.jucai.pppeiyouclient.bean.OrderBean;
import com.jucai.pppeiyouclient.comm.CommonRequestNetData;
import com.jucai.pppeiyouclient.utils.LogUtils;
import com.jucai.pppeiyouclient.utils.ToastUtils;
import com.jucai.pppeiyouclient.view.activity.CommonH5Activity;
import com.jucai.pppeiyouclient.weidget.pulltorefresh.PullToRefreshForRecyclerView;
import com.jucai.pppeiyouclient.weidget.pulltorefresh.PullToRefreshLayout;

import org.json.JSONArray;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

/**
 * Created by Kuky on 2017-04-21.
 */

public class RunningOrdersFragment extends BaseFragment implements PullToRefreshLayout.OnRefreshListener {
    private static final String TAG = "RunningOrdersFragment : ";
    private PullToRefreshLayout refreshLayout;
    private PullToRefreshForRecyclerView runningOrdersRv;
    private List<OrderBean> orders = new ArrayList<>();
    private RunningOrderAdapter orderAdapter;
    private Handler mHandler = new Handler();
    private int page = 1;

    @Override
    protected void initView() {
        orderAdapter = new RunningOrderAdapter(getActivity(), orders);
        refreshLayout = (PullToRefreshLayout) itemView.findViewById(R.id.refresh_view);
        runningOrdersRv = (PullToRefreshForRecyclerView) itemView.findViewById(R.id.content_view);
        runningOrdersRv.setLayoutManager(new LinearLayoutManager(getActivity(), LinearLayoutManager.VERTICAL, false));
        runningOrdersRv.setItemAnimator(new DefaultItemAnimator());
        runningOrdersRv.setAdapter(orderAdapter);
        orderAdapter.setOnItemClickListener(new CommonRecyclerViewAdapter.OnItemClickListener() {
            @Override
            public void onItemClickListener(View view, int position) {
                OrderBean o = orders.get(position);
                if (!TextUtils.isEmpty(o.getURL())) {
                    CommonH5Activity.startCommH5(getActivity(), 1, o.getURL(), null, o.getTorder_id(), null);
                    LogUtils.e(TAG + "url = " + o.getURL());
                }
            }

            @Override
            public void onItemLongClickListener(View view, int position) {

            }
        });
    }

    @Override
    protected void initData() {
    }

    @Override
    protected void loadNetData() {
        HashMap<String, String> param = new HashMap<>();
        param.put("service", "Conductor_Order.getlistuid");
        param.put("status", "3");
        param.put("page", "1");
        LogUtils.e(TAG + "param = " + param.toString());
        CommonRequestNetData.getJsonArrayData(getActivity(), param, true, new CommonRequestNetData.JsonArrayCallBack() {
            @Override
            public void parseJsonArray(JSONArray jsonArray) {
                try {
                    LogUtils.e(TAG + "jsonArray = " + jsonArray.toString());
                    List<OrderBean> listOrders = new Gson().fromJson(jsonArray.toString(), new TypeToken<List<OrderBean>>() {
                    }.getType());
                    if (page == 1)
                        orders.clear();
                    if (listOrders != null && listOrders.size() > 0) {
                        orders.addAll(listOrders);
                    }
                    orderAdapter.setData(orders);

                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        });
    }

    @Override
    protected void setListener() {
        refreshLayout.setOnRefreshListener(this);
    }

    @Override
    protected int getFragmentViewId() {
        return R.layout.common_fragment_with_ptr;
    }

    @Override
    public void onClick(View view) {

    }

    @Override
    public void onRefresh(final PullToRefreshLayout pullToRefreshLayout) {
        mHandler.postDelayed(new Runnable() {
            @Override
            public void run() {
                loadNetData();
                pullToRefreshLayout.refreshFinish(PullToRefreshLayout.SUCCEED);
            }
        }, 500);
    }

    @Override
    public void onLoadMore(final PullToRefreshLayout pullToRefreshLayout) {
        ++page;
        mHandler.postDelayed(new Runnable() {
            @Override
            public void run() {
                HashMap<String, String> param = new HashMap<>();
                param.put("service", "Conductor_Order.getlistuid");
                param.put("status", "3");
                param.put("page", page + "");
                LogUtils.e(TAG + "param = " + param.toString());
                CommonRequestNetData.getJsonArrayData(getActivity(), param, true, new CommonRequestNetData.JsonArrayCallBack() {
                    @Override
                    public void parseJsonArray(JSONArray jsonArray) {
                        try {
                            LogUtils.e(TAG + "jsonArray = " + jsonArray.toString());
                            List<OrderBean> list = new Gson().fromJson(jsonArray.toString(), new TypeToken<List<OrderBean>>() {
                            }.getType());
                            if (list != null && list.size() > 0) {
                                orders.addAll(list);
                                orderAdapter.setData(orders);
                            } else
                                ToastUtils.showToast("没有更多数据~");
                        } catch (Exception e) {
                            e.printStackTrace();
                        }
                    }
                });
                pullToRefreshLayout.loadmoreFinish(PullToRefreshLayout.SUCCEED);
            }
        }, 500);
    }
}
